<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Human resource pro
*
* Author: Lucky Sharma
*         jumboteam726@gmail.com
*
* Website: http://www.jumboteam.net
*
* Created:  03.25.2014
*/

class admin_vacation_model extends CI_Model {

    /**
     * Limit
     *
     * @var string
     * */
    public $_ion_limit = NULL;

    /**
     * Offset
     *
     * @var string
     * */
    public $_ion_offset = NULL;

    /**
     * message (uses lang file)
     *
     * @var string
     * */
    protected $messages;

    /**
     * error message (uses lang file)
     *
     * @var string
     * */
    protected $errors;

    /**
     * error start delimiter
     *
     * @var string
     * */
    protected $error_start_delimiter;

    /**
     * error end delimiter
     *
     * @var string
     * */
    protected $error_end_delimiter;

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->lang->load('admin_estate');
        $this->load->helper('date');

        //Load the session, CI2 as a library, CI3 uses it as a driver
        if (substr(CI_VERSION, 0, 1) == '2') {
            $this->load->library('session');
        } else {
            $this->load->driver('session');
        }

        //initialize messages and error
        $this->messages = array();
        $this->errors = array();
        $this->message_start_delimiter = "<p>";
        $this->message_end_delimiter = "</p>";
        $this->error_start_delimiter = "<p>";
        $this->error_end_delimiter = "</p>";
    }

    
    public function getVacation($id = null, $whr = NULL, $order = NULL, $select = NULL) {
        if ($id != NULL) {
            $this->db->select('*,vacations.id as vacations_id,staffusers.id as user_id');
            $this->db->from('vacations');
            $this->db->join('staffusers', 'vacations.user_id = staffusers.id', 'LEFT');
            
            $query = $this->db->where('id', $id)->get()->result();
            
        } else {
            if ($select != NULL) {
                $this->db->select($select);
            } else {
                $this->db->select('*,vacations.id as vacations_id,staffusers.id as user_id');
            }
            $this->db->from('vacations');
            $this->db->join('staffusers', 'vacations.user_id = staffusers.id', 'LEFT');
            
            if ($whr != NULL) {
                $this->db->where($whr);
            } else {
                //$this->db->where('publish', 1);
            }
            if ($order != NULL) {
                $this->db->order_by('id', 'DESC');
            }
            if (($this->_ion_limit != NULL) or ($this->_ion_offset != NULL)) {
                $this->db->limit($this->_ion_limit, $this->_ion_offset);
                $this->_ion_limit = NULL;
                $this->_ion_offset = NULL;
            }

            $query = $this->db->get()->result();
            
        }
       
        //echo "<BR>".$this->db->last_query();
        return $query;
    }
    
    public function num_row_vacation($whr = NULL) {
        if ($whr != NULL) {
            $this->db->where($whr);
        } 
        return $this->db->get('vacations')->num_rows();
    }
    
    
    public function vacationInsert($value = null) {
        if ($value != NULL) {
            /*$data = array(
                'timesheets' => $value
            );*/
        	$value['created'] = date('Y-m-d H:i:s');
        	
            $query = $this->db->insert('vacations', $value);
        } else {
            $this->set_error('estate_type_error');
            return false;
        }
        $this->set_message('estate_type_success');
        return true;
    }

    public function vacationUpdate($value = null, $id = NULL) {
        if ($value != NULL and $id != NULL) {
            /*$data = array(
                'timesheets' => $value
            );*/
            $this->db->where('id', $id);
            
            $value['modified'] = date('Y-m-d H:i:s');
            $query = $this->db->update('vacations', $value);
        } else {
            $this->set_error('estate_type_error');
            return false;
        }
        $this->set_message('estate_type_success');
        return true;
    }
    
    public function setStatus($status = 1,$id = null) {
        if ($id != NULL) {
            $this->db->where('id', $id);
            $this->db->update('vacations', array('approval_status_id' => $status));
            //echo "<BR>".$this->db->last_query();exit;
            return TRUE;
        } else {
            return false;
        }
    }
    
    /**
     * set_message_delimiters
     *
     * Set the message delimiters
     *
     * @return void
     * @author Ben Edmunds
     * */
    public function set_message_delimiters($start_delimiter, $end_delimiter) {
        $this->message_start_delimiter = $start_delimiter;
        $this->message_end_delimiter = $end_delimiter;

        return TRUE;
    }

    /**
     * set_error_delimiters
     *
     * Set the error delimiters
     *
     * @return void
     * @author Ben Edmunds
     * */
    public function set_error_delimiters($start_delimiter, $end_delimiter) {
        $this->error_start_delimiter = $start_delimiter;
        $this->error_end_delimiter = $end_delimiter;

        return TRUE;
    }

    /**
     * set_message
     *
     * Set a message
     *
     * @return void
     * @author Ben Edmunds
     * */
    public function set_message($message) {
        $this->messages[] = $message;

        return $message;
    }

    /**
     * messages
     *
     * Get the messages
     *
     * @return void
     * @author Ben Edmunds
     * */
    public function messages() {
        $_output = '';
        foreach ($this->messages as $message) {
            $messageLang = $this->lang->line($message) ? $this->lang->line($message) : '##' . $message . '##';
            $_output .= $this->message_start_delimiter . $messageLang . $this->message_end_delimiter;
        }

        return $_output;
    }

    /**
     * messages as array
     *
     * Get the messages as an array
     *
     * @return array
     * @author Raul Baldner Junior
     * */
    public function messages_array($langify = TRUE) {
        if ($langify) {
            $_output = array();
            foreach ($this->messages as $message) {
                $messageLang = $this->lang->line($message) ? $this->lang->line($message) : '##' . $message . '##';
                $_output[] = $this->message_start_delimiter . $messageLang . $this->message_end_delimiter;
            }
            return $_output;
        } else {
            return $this->messages;
        }
    }
    
    /**
     * set_error
     *
     * Set an error message
     *
     * @return void
     * @author Ben Edmunds
     * */
    public function set_error($error) {
        $this->errors[] = $error;

        return $error;
    }

    /**
     * errors
     *
     * Get the error message
     *
     * @return void
     * @author Ben Edmunds
     * */
    public function errors() {
        $_output = '';
        foreach ($this->errors as $error) {
            $errorLang = $this->lang->line($error) ? $this->lang->line($error) : '##' . $error . '##';
            $_output .= $this->error_start_delimiter . $errorLang . $this->error_end_delimiter;
        }

        return $_output;
    }

    /**
     * errors as array
     *
     * Get the error messages as an array
     *
     * @return array
     * @author Raul Baldner Junior
     * */
    public function errors_array($langify = TRUE) {
        if ($langify) {
            $_output = array();
            foreach ($this->errors as $error) {
                $errorLang = $this->lang->line($error) ? $this->lang->line($error) : '##' . $error . '##';
                $_output[] = $this->error_start_delimiter . $errorLang . $this->error_end_delimiter;
            }
            return $_output;
        } else {
            return $this->errors;
        }
    } 


    ////////////
    public function num_row_selectbox() {
        return $this->db->get('selectbox')->num_rows();
    }

    public function num_row_getName($table_name, $where = NULL) {
        if ($where != NULL) {
            $this->db->where($where);
        }
        return $this->db->get($table_name)->num_rows();
    }

    public function num_row_category() {
        return $this->db->get('category')->num_rows();
    }

    public function num_row_type() {
        return $this->db->get('estatetype')->num_rows();
    }

    public function paginationLimit($limit, $offset) {
        $this->_ion_limit = $limit;
        $this->_ion_offset = $offset;
    }  
    
    public function vacationEdit($id) {
       if ($id) {
           $this->db->select('*');
           $this->db->from('vacations');
           /*$this->db->join('users', 'timesheets.user_id = users.id', 'LEFT');
           $this->db->join('jobs', 'timesheets.job_id = jobs.id', 'LEFT');*/
           $query = $this->db->where('vacations.id', $id)->get()->result();
       }
       
       return $query;
       
   }
   
   public function hrmVacation($id = NULL) {
            
        if ($id != NULL) {
            $this->db->select('*');
            $this->db->from('vacations');
            //$query = $this->db->where('user_id', $id)->get()->result_array();
            $query = $this->db->get()->result_array();
			//$query = $this->db->get()->result();
        }else{
            $this->db->select('*');
            $this->db->from('vacations');
            $query = $this->db->get()->result_array();
            //echo "<BR>".$this->db->last_query();exit;
        }
        
        return $query;
    }
}